#!/usr/bin/perl -w
#
# $Id: Observable.pm,v 1.9 2004/03/15 03:43:43 solovam Exp $
#
# This file is a part of gkismet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

#
# Observable class
#
package Observable;

use strict;

#
# Notify observers
#
sub notifyObservers
{
	my $self = shift;
	my $data = shift;
	for (my $i = 0; $i < scalar(@{$self->{'observers'}}); $i++)
	{
		$self->{'observers'}[$i]->update($self, $data);
	}
}

#
# Register an observer
#
sub addObserver
{
	my $self = shift;
	my $observer = shift;
	push @{$self->{'observers'}}, $observer;
}

#
# Remove observer
#
sub removeObserver
{
	my $self = shift;
	my $observer = shift;
	for(my $i = 0; $i < scalar(@{$self->{'observers'}}); $i++)
	{
		if($self->{'observers'}[$i] == $observer)
		{
			splice @{$self->{'observers'}}, $i, 1;
		}
	}
}

1;
